﻿using ObjCore.Interface;
using System.Collections.Generic;
using ObjCore.MOgreExt;

namespace ObjCore.Impl
{
    public class Loader : ILoader
    {
        /// <summary>
        /// 模型顶点坐标集合
        /// </summary>
        IList<string> header = new List<string>();
        IList<Mogre.Vector3> v = new List<Mogre.Vector3>();
        IList<Mogre.Vector2> vt = new List<Mogre.Vector2>();
        IList<Mogre.Vector3> vn = new List<Mogre.Vector3>();
        IList<string> f = new List<string>();

        public IList<Mogre.Vector3> V => v;
        public IList<Mogre.Vector2> VT => vt;
        public IList<Mogre.Vector3> VN => vn;

        /// <summary>
        /// 材质库
        /// </summary>
        string mtllib;

        void Clear()
        {
            header.Clear();
            v.Clear();
            vt.Clear();
            vn.Clear();
            f.Clear();
        }

        public void Open(string path)
        {
            using(var reader = new System.IO.StreamReader(path))
            {
                Clear();

                string line = null;
                do
                {
                    line = reader.ReadLine();

                    if (line == null)
                    {
                        break;
                    }

                    if (!line.StartsWith("v"))
                    {
                        header.Add(line);
                    }
                    else
                    {
                        break;
                    }

                } while (line != null);

                do
                {
                    if (line == null)
                    {
                        break;
                    }

                    ParseLine(line);
                    line = reader.ReadLine();
                } while (line != null);
            }
        }

        public void SaveAs(string path)
        {
            using (var writer = new System.IO.StreamWriter(path))
            {
                foreach (var item in header)
                {
                    writer.WriteLine(item);
                }

                foreach (var item in v)
                {
                    writer.WriteLine(item.ToV());
                }

                foreach (var item in vt)
                {
                    writer.WriteLine(item.ToVt());
                }

                foreach (var item in vn)
                {
                    writer.WriteLine(item.ToVn());
                }

                foreach (var item in f)
                {
                    writer.WriteLine(item);
                }
            }
        }

        void ParseLine(string line)
        {
            if (line.StartsWith("#"))
            {
                return;
            }

            if (ParseV(line))
            {
                return;
            }

            if (ParseVt(line))
            {
                return;
            }
            if (ParseVn(line))
            {
                return;
            }
            if (Parsef(line))
            {
                return;
            }
        }

        bool ParseV(string line)
        {
            if (!line.StartsWith("v") || line.StartsWith("vt") || line.StartsWith("vn"))
            {
                return false;
            }

            v.Add(line.ParseV());

            return true;
        }

        bool ParseVt(string line)
        {
            if (!line.StartsWith("vt"))
            {
                return false;
            }

            vt.Add(line.ParseVt());

            return true;
        }

        bool ParseVn(string line)
        {
            if (!line.StartsWith("vn"))
            {
                return false;
            }

            vn.Add(line.ParseVn());

            return true;

        }

        bool Parsef(string line)
        {
            f.Add(line);
            return true;
        }

    }
}
